﻿using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using Graphix;
using Graphix.Charts;

namespace ConsoleTest
{
    using System.Diagnostics;

    using Graphix.Charts.Values;
    using Graphix.Elements;

    class Program
    {
        static void Main(string[] args)
        {
            DrawGaugeChart();
            DrawColumnChart();
            DrawBarChart();
            BubbleChart();
            DrawScatterChart();

            CandlestickChart();
            DrawPieChart();
        }

        static int loop = 1;//4;
        static int loop2 = 1;//8;
        private static void BubbleChart()
        {
            for (int i = 0; i < loop; i++)
            {
                BubbleChart bc = new BubbleChart();
                bc.Height = 500;
                bc.Width = 500;
                bc.YAxis.Title.Text = "YAxis";
                bc.Title = new ValuedTextStyle(text: "Title") { Alignment = Horizontal.Right };
                bc.XAxis.Title.Text = "XAxis";
                bc.XAxis.OppositeDirection = display[i][0];
                bc.YAxis.OppositeDirection = display[i][1];
                bc.LegendSettings.Position = Position.MiddleRight;
                bc.XAxis.DisplayValues = display[i][0];
                bc.YAxis.DisplayValues = display[i][1];
                bc.XAxis.Format = "{0:# #}";
                //bc.PossibleColors = new Color[] { Color.YellowGreen, Color.YellowGreen, Color.YellowGreen };
                //['Mon', 20, 28, 38, 45],
                //['Tue', 31, 38, 55, 66],
                //['Wed', 50, 55, 77, 80],
                //['Thu', 77, 77, 66, 50],
                //['Fri', 68, 66, 22, 15]
                bc.Labels = new[] { "Label1", "Label2", "Label3", "label4" };
                bc.AddSeries("A", new List<Bubble>()
                    {
                        new Bubble("A", 0, 0, 45),
                        new Bubble("B", 31, 38, 66),
                    });
                bc.AddSeries("B", new List<Bubble>()
                    {
                        new Bubble("C", 50, 55, 100),
                        new Bubble("D", 77, 77, 50),
                        new Bubble("E", 68, 66, 15)
                    });
                bc.DisplayLegend = true;

                bc.AddJavascript = true;
                string textpc = bc.Save();
                StreamWriter filepc = new StreamWriter(string.Format("BubbleChart{0}.html", i));
                filepc.WriteLine(textpc);
                filepc.Close();

                bc.SaveToFile(string.Format("BubbleChart{0}.png", i), ImageFormat.Png);
            }
        }

        private static bool[][] display = new bool[4][]
            {
                new[] { true, true },
                new[] { true, false },
                new[] { false, true },
                new[] { false, false }
            };

        private static void CandlestickChart()
        {
            for (int i = 0; i < loop; i++)
            {
                CandlestickChart bc = new CandlestickChart();
                bc.Height = 500;
                bc.Width = 500;
                bc.Title.Text = "Title";
                //bc.TextStyle.Font = new Font("Arial", 6);
                bc.Title.Font = new Font("Arial", 28);
                bc.XAxis.Title.Font = new Font("Arial", 18);
                bc.Title.Alignment = Horizontal.Right;
                bc.YAxis.Title.Text = "YAxis";
                bc.XAxis.Title.Text = "XAxis";
                bc.YAxis.OppositeDirection = display[i][0];
                bc.XAxis.OppositeDirection = display[i][1];
                bc.YAxis.GridlinesCount = 11;
                bc.LegendSettings.Position = Position.BottomRight;
                //bc.PossibleColors = new Color[] { Color.YellowGreen, Color.YellowGreen, Color.YellowGreen };
                //['Mon', 20, 28, 38, 45],
                //['Tue', 31, 38, 55, 66],
                //['Wed', 50, 55, 77, 80],
                //['Thu', 77, 77, 66, 50],
                //['Fri', 68, 66, 22, 15]

                bc.AddSets(new List<string> { "Mon", "Tue", "Wed", "Thu", "Fri" });
                bc.AddSeries("a", new List<Candle>
                    {
                        new Candle(20, 28, 38, 45),
                        new Candle(31, 38, 55, 66),
                        new Candle(50, 55, 77, 80),
                        new Candle(77, 77, 66, 50),
                        new Candle(68, 66, 22, 15)
                    });
                bc.AddSeries("b", new List<Candle>
                    {
                        new Candle(68, 66, 22, 15),
                        new Candle(68, 66, 22, 15),
                        new Candle(68, 66, 22, 15),
                        new Candle(68, 66, 22, 15),
                        new Candle(68, 66, 22, 15)
                    });
                bc.AddSeries("c", new List<Candle>
                    {
                        new Candle(68, 66, 22, 15),
                        new Candle(68, 66, 22, 15),
                        new Candle(68, 66, 22, 15),
                        new Candle(68, 66, 22, 15),
                        new Candle(68, 66, 22, 15)
                    });
                bc.DisplayLegend = true;

                bc.AddJavascript = true;
                string textpc = bc.Save();
                StreamWriter filepc = new StreamWriter(string.Format("CandleChart{0}.html", i));
                filepc.WriteLine(textpc);
                filepc.Close();

                bc.SaveToFile(string.Format("CandleChart{0}.png", i), ImageFormat.Png);
            }
        }

        static bool[][] test = new bool[8][]
                {
                    new bool[] { true, true, true },
                    new bool[] { true, true, false },
                    new bool[] { true, false, true },
                    new bool[] { true, false, false },
                    new bool[] { false, true, true },
                    new bool[] { false, true, false },
                    new bool[] { false, false, true },
                    new bool[] { false, false, false }
                };

        private static void DrawColumnChart()
        {
            for (int i = 0; i < 8; i++)
            {

                ColumnChart bc = new ColumnChart();
                bc.Height = 500;
                bc.Width = 500;
                bc.Background.Stroke = Color.Black;
                bc.Background.StrokeWidth = 1f;
                //bc.Title.Text = "Title";
                bc.Title.Alignment = Horizontal.Right;
                bc.XAxis.Title.Text = "XAxis";
                bc.YAxis.Title.Text = "YAxis";
                bc.XAxis.DisplayValues = i % 2 == 0;
                bc.YAxis.DisplayValues = false;
                bc.LegendSettings.Position = Position.BottomLeft;
                //bc.PossibleColors = new Color[] { Color.YellowGreen, Color.YellowGreen, Color.YellowGreen };
                //bc.YAxis.OppositeDirection = true;
                //['Year', 'Austria', 'Bulgaria', 'Denmark', 'Greece'],
                //['2003',  1336060,    ,    ,   ],
                //['2004',  1538156,    ,    ,   ],
                //['2005',  1576579,    ,    ,    ],
                //['2006',  1600652,    ,    ,   ],
                //['2007',  1968113,    ,    ,    ],
                //['2008',  ,    517206,    ,    1056036],
                //[,1901067,    517206,    916965,    1056036],
                //[,1901067,    517206,    916965,    ]

                bc.AddSets(new List<string>() { "2003", "2004", "2005", "2006", "2007", "2008" });
                bc.AddSeries("Austria",
                    new List<float>()
                        { 1336060, 1538156, 1576579, 1600652, 1968113, 1901067, 1901067, 1901067 });
                bc.AddSeries("Bulgaria",
                    new List<float>()
                        { 400361, 366849, 440514, 434552, 393032, 517206, 517206, 517206 });
                bc.AddSeries("Denmark",
                    new List<float>()
                        { 1001582, 1119450, 993360, 1004163, 979198, 916965, 916965, 916965 });
                bc.AddSeries("Greece",
                    new List<float>()
                        { 997974, 941795, 930593, 897127, 1080887, 1056036, 1056036, 1056036 });

                bc.DisplayLegend = true;
                //bc.IsStacked = true;


                bc.AddJavascript = true;


                bc.IsStacked = test[i][0];
                bc.XAxis.OppositeDirection = test[i][1];
                bc.YAxis.OppositeDirection = test[i][2];

                string textpc = bc.Save();
                StreamWriter filepc = new StreamWriter(string.Format("ColumnChart{0}.html", i));
                filepc.WriteLine(textpc);
                filepc.Close();

                bc.SaveToFile(string.Format("ColumnChart{0}.png", i), ImageFormat.Png);
            }

            //bc.SaveToFile("ColumnChart.png", ImageFormat.Png);
        }

        private static void DrawBarChart()
        {
            for (int i = 0; i < loop2; i++)
            {
                BarChart bc = new BarChart();

                bc.IsStacked = test[i][0];
                bc.XAxis.OppositeDirection = test[i][1];
                bc.YAxis.OppositeDirection = test[i][2];

                bc.Height = 500;
                bc.Width = 500;
                bc.Title.Text = "Title";
                bc.Title.Alignment = Horizontal.Right;
                bc.YAxis.Title.Text = "YAxis";
                bc.YAxis.Title.Alignment = Horizontal.Left;
                bc.XAxis.Title.Text = "abc";
                bc.XAxis.Title.Alignment = Horizontal.Left;
                //bc.PossibleColors = new Color[] { Color.YellowGreen, Color.YellowGreen, Color.YellowGreen };
                //['Year', 'Austria', 'Bulgaria', 'Denmark', 'Greece'],
                //['2003',  1336060,    ,    ,   ],
                //['2004',  1538156,    ,    ,   ],
                //['2005',  1576579,    ,    ,    ],
                //['2006',  1600652,    ,    ,   ],
                //['2007',  1968113,    ,    ,    ],
                //['2008',  ,    517206,    ,    1056036],
                //[,1901067,    517206,    916965,    1056036],
                //[,1901067,    517206,    916965,    ]

                bc.AddSets(new List<string>() { "2003", "2004", "2005", "2006", "2007", "2008" });
                bc.AddSeries("Austria",
                    new List<float>()
                        { 1336060, 1538156, 1576579, 1600652, 1968113, 1901067, 1901067, 1901067 });
                bc.AddSeries("Bulgaria",
                    new List<float>()
                        { 400361, 366849, 440514, 434552, 393032, 517206, 517206, 517206 });
                bc.AddSeries("Denmark",
                    new List<float>()
                        { 1001582, 1119450, 993360, 1004163, 979198, 916965, 916965, 916965 });
                //bc.AddSeries("Greece",
                //    new List<float>()
                //        { 997974, 941795, 930593, 897127, 1080887, 1056036, 1056036, 1056036 });

                bc.DisplayLegend = true;
                //bc.IsStacked = true;

                bc.AddJavascript = true;
                string textpc = bc.Save();
                StreamWriter filepc = new StreamWriter(string.Format("BarChart{0}.html", i));
                filepc.WriteLine(textpc);
                filepc.Close();
                bc.SaveToFile(string.Format("BarChart{0}.png", i), ImageFormat.Png);
            }
            //bc.SaveToFile("BarChart.png", ImageFormat.Png);
        }

        private static void DrawScatterChart()
        {
            for (int i = 0; i < loop; i++)
            {
                ScatterChart sc = new ScatterChart();

                sc.ScaleNicely = true;
                sc.Height = 500;
                sc.Width = 500;
                sc.Title = new ValuedTextStyle(text: "title") { Alignment = Horizontal.Right };
                sc.XAxis.Title.Text = "XAxis";
                sc.YAxis.Title.Text = "YAxis";
                sc.XAxis.OppositeDirection = display[i][0];
                sc.YAxis.OppositeDirection = display[i][1];
                sc.XAxis.GridlinesCount = 20;
                sc.LegendSettings.Position = Position.BottomRight;
                //sc.PossibleColors = new Color[] { Color.YellowGreen, Color.YellowGreen, Color.YellowGreen };
                /*8,      12],
              [ 4,      5.5],
              [ 11,     14],
              [ 4,      5],
              [ 3,      3.5],
              [ 6.5,    7]*/
                sc.AddData(-8, -12);
                sc.AddData(-4, -5.5f);
                sc.AddData(-11, -14);
                sc.AddData(-4, -5);
                sc.AddData(-3, -3.5f);
                sc.AddData(-6.5f, -7);
                /*
            sc.AddData(1, 0);
            sc.AddData(1, -5);
            sc.AddData(0, 5);
            sc.AddData(-1, 15);
            sc.AddData(-2, 10);
            sc.AddData(-2, new List<float>() { 1, 2 });
                */
                //sc.AddData(0, 1);
                //sc.AddData(-5, 0);
                //sc.AddData(5, 0);
                //sc.AddData(15, -1);
                //sc.AddData(10, -2);
                //sc.AddData(1, -2);
                //sc.AddData(2, -2);

                sc.AddYSeriesName("abc1");
                sc.AddYSeriesName("abc2");
                sc.AddYSeriesName("abc3");
                sc.AddYSeriesName("abc4");

                sc.XAxis.Minimum = 0;
                sc.XAxis.Maximum = 10;
                sc.YAxis.Minimum = 0;
                sc.YAxis.Maximum = 20;

                //sc.BackgroundColor = Color.White;
                sc.AddJavascript = true;
                sc.DisplayLegend = true;

                string textpc = sc.Save();

                StreamWriter filepc = new StreamWriter(string.Format("ScatterChart{0}.html", i));
                filepc.WriteLine(textpc);
                filepc.Close();

                sc.SaveToFile(string.Format("ScatterChart{0}.png", i), ImageFormat.Png);
            }
        }

        private static void DrawPieChart()
        {
            PieChart pc = new PieChart();
            pc.Height = 500;
            pc.Width = 500;
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.AddData("50", 50);
            pc.Title = new ValuedTextStyle("Arial", 16, Color.Black) { Text = "Title" };
            pc.LegendSettings.TextStyle.Font = new Font("Arial", 8);
            pc.DisplayLegend = true;
            // pc.DisplayMode = DisplayMode.Percentage;
            string textpc = pc.Save();

            StreamWriter filepc = new StreamWriter(string.Format("testPie.html"));
            filepc.WriteLine(textpc);
            filepc.Close();
            pc.SaveToFile("testPie.png", ImageFormat.Png);
        }

        private static void DrawGaugeChart()
        {
            for (int i = 400; i <= 500; i += 50)
            {
                var gauge = ChartHelper.GaugeChart();
                gauge.Minimum = -100;
                gauge.Maximum = 100;
                gauge.Height = i;
                gauge.Width = i;
                gauge.GreenFrom = -100;
                gauge.GreenTo = 60;
                gauge.YellowFrom = 60;
                gauge.YellowTo = 80;
                //gauge.RedFrom = 80;
                gauge.RedTo = 100;
                gauge.Values = new List<float>() { 0, 40 };
                gauge.AddJavascript = true;
                gauge.Easing = Easing.EaseInQuint;

                var text = gauge.Save();

                StreamWriter file = new StreamWriter(string.Format("testGauge{0}.html", i));
                file.WriteLine(text);
                file.Close();
                gauge.SaveToFile(string.Format("testGauge{0}.png", i), ImageFormat.Png);
            }
        }

        private static void DrawLineChart()
        {
            string[] xlabels =
                {
                    "18", "19", "20", "21",
                    "22", "23", "24", "1",
                    "2", "3"
                };
            string[] x2labels =
                {
                    "5.60", "3.82", "2.90", "2.36",
                    "1.99", "1.74", "1.55", "1.41",
                    "1.30", "1.22", "1.15", "1.10",
                    "1.06", "1.04", "1.02"
                };

            string[] ylabels = StaticHelpers.GenerateFullDegrees(0, 90, 5);
            string[] y2labels = new string[]
                                    {
                                        "23.24", "0.25", "1.25", "2.25", "3.25", "4.25", "5.25",
                                        "6.26"
                                    };
            Font f = new Font("Arial", 10, FontStyle.Regular);
            Pen _dotted = new Pen(Brushes.Black);
            _dotted.DashStyle = DashStyle.Dot;


            /*var temp = */
            LineChart lc = ChartHelper.LineChart(400, 400, Brushes.Black)
                .SetYMin(0)
                .SetYMax(90)
                .SetBackground(Color.MistyRose)
                .SetTitle("Altitudes, Sutherland Observatoryʰ", f, Horizontal.Left)
                .SetTitle("20.8117E -32.3783N, 1771 m above sea level", f, Horizontal.Right,
                    offset: -0.1f)
                .SetX2Labels(y2labels, f, Brushes.Red, 0.16, 0.86)
                .AddTitleMarginMarker(
                    string.Format("S.set\n{0}", StaticHelpers.BuildHour("17", "12")), 0, _dotted,
                    f)
                .AddTitleMarginMarker(
                    string.Format("Twil\n{0}", StaticHelpers.BuildHour("18", "36")), 0.12, _dotted,
                    f)
                .AddTitleMarginMarker(
                    string.Format("Twil\n{0}", StaticHelpers.BuildHour("2", "4")), 0.87, _dotted,
                    f)
                .AddTitleMarginMarker(
                    string.Format("S.rise\n{0}", StaticHelpers.BuildHour("3", "28")), 1, _dotted,
                    f, moreMarkers: false)
                .AddInformation(
                    "Moon (dashed):\nCoordinates:\n8ʰ12ᵐ + 14°57′\nIllumination   60%\nQuarter     3\n \n \nNumbers below curves\nare Moon Distance\n(in degres) at the\ncorresponding\ntimes.",
                    f, Brushes.Fuchsia)
                .SetYTitle("Altitude", f)
                .SetYLabels(ylabels, _dotted, f)
                .SetXLabels(xlabels, _dotted, f, min: 0.06)
                .SetXTitle("title", f)
                .SetXTitle("title2", f)
                .SetY2Labels(x2labels, f, Brushes.Black, (100.0 / 9.0 / 100.0),
                    (1.0 - (100.0 / 9.0 / 100.0)))
                .DrawCurve(new[]
                    {
                        new PointF(0, 48),
                        new PointF(0.06f, 55),
                        new PointF(0.16f, 66),
                        new PointF(0.26f, 73),
                        new PointF(0.36f, 74),
                        new PointF(0.46f, 70),
                        new PointF(0.56f, 60.5f),
                        new PointF(0.66f, 50),
                        new PointF(0.76f, 40),
                        new PointF(0.86f, 29.5f),
                        new PointF(0.96f, 20),
                        new PointF(1, 16.5f)
                    },
                    "objectName", ValueType.XPercentage | ValueType.YValues,
                    penSize: 4);
            string svg = lc.Save();
            lc.SaveToFile("LineChart.png", ImageFormat.Png);

            StreamWriter file = new StreamWriter("LineChart.html");
            file.WriteLine(svg);

            file.Close();

            //.SaveToStream(Response.OutputStream, ImageFormat.Png);
        }
    }
}
